package com.cardbaobao.cardbabyclient.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.ui.activity.CreditCardRaidersActivity;
import com.cardbaobao.cardbabyclient.callback.RecycleItemClickedCallback;
import com.cardbaobao.cardbabyclient.model.CreditCardRaidersLayout;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;

import java.util.List;

/**
 * Created by Jiangzx on 9:22.
 */

public class WoolRaidersDelegateAdapter extends DelegateAdapter.Adapter<RecyclerView.ViewHolder> {
    private RecycleItemClickedCallback clickedCallback;
    public void setRecycleItemClickedCallback(RecycleItemClickedCallback clickedCallback){
        this.clickedCallback=clickedCallback;
    }
    private LayoutHelper layoutHelper;
    private List<CreditCardRaidersLayout.WoolRaiders> data;
    public WoolRaidersDelegateAdapter(LayoutHelper layoutHelper, List<CreditCardRaidersLayout.WoolRaiders> data){
       this.layoutHelper=layoutHelper;
        this.data=data;
   }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return layoutHelper;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).
                inflate(R.layout.item_wool_raiders,parent,false);
        return new WoolRaidersHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        CreditCardRaidersLayout.WoolRaiders woolRaiders=data.get(position);
        WoolRaidersHolder woolHolder= (WoolRaidersHolder) holder;
        woolHolder.tvTitle.setText(woolRaiders.getName());
        int positionMode=position%3;
        switch (positionMode){
            case 0:
                woolHolder.tvDate.setBackgroundResource(R.drawable.shape_corner_1dp_solid_ffbd18);
                break;
            case 1:
                woolHolder.tvDate.setBackgroundResource(R.drawable.shape_corner_1dp_solid_fa864b);
                break;
            case 2:
                woolHolder.tvDate.setBackgroundResource(R.drawable.shape_corner_1dp_solid_7dd66e);
                break;
        }
        String text=woolRaiders.getDay();
        woolHolder.tvDate.setText(text);
        if("今天".equals(text)){
            woolHolder.tvDate.setBackgroundResource(R.drawable.shape_corner_1dp_solid_f72d48);
            woolHolder.ivPromise.setVisibility(View.VISIBLE);
        }else{
            woolHolder.ivPromise.setVisibility(View.INVISIBLE);
        }
        woolHolder.tvTitle.setText(woolRaiders.getName());
        woolHolder.tvContent.setText(woolRaiders.getMarketing_one());
        woolHolder.tvContentBottom.setText(woolRaiders.getMarketing_two());
        woolHolder.tvViewCount.setText(String.valueOf(woolRaiders.getNumber()));
        ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL+woolRaiders.getPicture_path(),
                woolHolder.imageView,
                R.mipmap.img_default_rectangle);
        woolHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(clickedCallback!=null){
                    clickedCallback.onRecycleItemClicked(position);
                }
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        return CreditCardRaidersActivity.RAIDERS_WOOL;
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    private class WoolRaidersHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView tvTitle;
        private ImageView ivPromise;
        private TextView tvDate;
        private TextView tvContent;
        private TextView tvContentBottom;
        private TextView tvViewCount;
        private WoolRaidersHolder(View itemView) {
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.image);
            tvTitle= (TextView) itemView.findViewById(R.id.tv_title);
            ivPromise= (ImageView) itemView.findViewById(R.id.iv_promise);
            tvDate= (TextView) itemView.findViewById(R.id.tv_date);
            tvContent= (TextView) itemView.findViewById(R.id.tv_content);
            tvContentBottom= (TextView) itemView.findViewById(R.id.tv_content_bottom);
            tvViewCount= (TextView) itemView.findViewById(R.id.tv_view_count);
        }
    }
}
